<template>
  <div class="root" :style="{background: `url('${bg}')`}">
    <div class="body">
      <div class="ev-container">
        <div class="label-title">相关评价</div>
        <ul class="rate-list">
          <li>
            <div>服务态度</div>
            <van-rate v-model="serveScore" allow-half />
          </li>
          <li>
            <div>酒店环境</div>
            <van-rate v-model="possyScore" allow-half />
          </li>
          <li>
            <div>餐饮美食</div>
            <van-rate v-model="totalScore" allow-half />
          </li>
        </ul>
      </div>
      <div class="ev-container">
        <div class="label-title">该酒店哪些让你印象深刻？</div>
        <ul class="feature-list">
          <li v-for="item in featureList" :class="featureSelected.includes(item) ? 'active' : ''"
            @click="handleSelectFeature(item)">{{ item }}</li>
        </ul>
        <div style="clear: both"></div>
        <div style="border: 1px solid #E7E7E7;border-radius: 4px;margin-top: 20px;">
          <van-field v-model="content" rows="4" autosize type="textarea" maxlength="50"
            placeholder="请填写您的建议，以便我们提供更好的服务！" show-word-limit />
        </div>
      </div>
    </div>
    <div class="footer">
      <div class="btn" @click="handleSubmit">
        提交
      </div>
    </div>
  </div>

</template>
<script setup>
import bg from '@/assets/img/bg.png'
import { ref, reactive, nextTick, onMounted, computed, watch } from 'vue'
import { showSuccessToast } from 'vant';
import { userApi } from '@/api'
import { useRouter, useRoute } from 'vue-router';

const siteName = ref()
const siteId = ref()
const serveScore = ref(0)
const possyScore = ref(0)
const totalScore = ref(0)
const featureList = ['位置便利，交通方便', '环境舒适', '设施合理', '人员热情，服务周到']
const featureSelected = ref([])
const content = ref('')
const route = useRoute()
const router = useRouter()

onMounted(() => {
    const { id, name, phone } = route.query
    siteName.value = name
    siteId.value = id
    
})

const handleSelectFeature = (id) => {
  const index = featureSelected.value.indexOf(id)
  if (index > -1) {
    featureSelected.value.splice(index, 1);
  } else {
    featureSelected.value.push(id);
  }
}
watch(featureSelected.value, (n, o) => {
  content.value = n.join('、')
})

const handleSubmit = () => {
  if(serveScore.value &&  possyScore.value && totalScore.value && content.value) {
    const uid = localStorage.getItem('uid')
    const payload = {
      evaluate: {
        content: content.value,
        score:  ((serveScore.value + possyScore.value + totalScore.value) / 3).toFixed(1),
      },
      id: siteId.value,
      uid: uid,
    }
    userApi.saveEvaluate(payload).then(res => {
      showSuccessToast('提交成功')
      router.back()
    })
  }
}


</script>
<style lang="less" scoped>
.root {
  height: 100vh;
  background-color: #f1f3f4;
  background-repeat: no-repeat;
  .body{
    height: calc(100vh - 64px);
    overflow: scroll;
  }
  .footer{
    background: #fff;
    padding: 12px;
    .btn {
      background: #EE5143;
      border-radius: 22px;
      color: #fff;
      flex: 1;
      height: 40px;
      line-height: 40px;
      text-align: center;
      font-weight: 600;
      font-size: 14px;
    }
  }
}

.ev-container {
  background: #FFFFFF;
  border-radius: 8px;
  margin: 12px;
  padding: 12px;

  .label-title {
    font-size: 16px;
    color: #333333;
    font-weight: bold;
    padding: 8px 0;
    border-bottom: 1px solid #F6F6F6;
  }

  .rate-list {
    li {
      display: flex;
      padding: 10px 0;

      div {
        margin-right: 10px;
        font-size: 14px;
      }
    }
  }

  .feature-list {
    li {
      border: 1px solid #E7E7E7;
      border-radius: 24px;
      height: 28px;
      line-height: 28px;
      float: left;
      padding: 0 25px;
      margin-right: 10px;
      margin-top: 10px;
      font-size: 14px;
      &.active {
        background: #ee51431a;
        border: 1px solid #ee514333;
        color: #EE5143;
      }
    }
  }
}
</style>